package com.bhxx.fund.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class MailServiceImpl implements MailService {
    private final static Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
    @Autowired
    private JavaMailSender mailSender;

    @Value("${mail.fromMail.addr}")
    private String fromMail;
    /**
     * 发送简单邮件
     * @param to 接受者
     * @param subject 主题
     * @param content 内容
     * @param sendNumber
     */

    @Override
    public void sendMail(String to, String subject, String content, int sendNumber) {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom(fromMail);
        mailMessage.setTo(to);
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        try {
            for (int i = 0; i < sendNumber; i++) {
                mailSender.send(mailMessage);
                logger.info("发送邮件成功，邮件内容{}",mailMessage);
            }

        }catch (Exception e){
            e.printStackTrace();
            logger.info("发送邮件失败，邮件内容{}",mailMessage);
        }
    }


}
